package ro.msg.quiz.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

//AUTHOR: Ervin

//View for adding possbile answers and checking the correct answer for a new question
@SuppressWarnings("serial")
public class MultipleChoiceWindow extends JFrame {
    private JLabel possibleAnswerLabel = new JLabel("Edit possible answers");

    private JButton back = new JButton("Back");
    private JButton cancel = new JButton("Cancel");
    private JButton finish = new JButton("Finish");
    private JButton[] boxes;

    private JTextField[] fields;

    private JPanel panel = new JPanel();

    public MultipleChoiceWindow(int nrOfOptions, String typeWindow) {

	int frameHeight = nrOfOptions * 40 + 150;
	this.setSize(new Dimension(480, frameHeight));
	this.setTitle("SmartQuest 1.0 - Add new question - " + typeWindow);

	// ////////////declare the nr of boxes and textfields for the possible
	// answers//////
	if (nrOfOptions < 2) {
	    boxes = new JButton[1];
	    fields = new JTextField[1];
	    nrOfOptions = 1;
	} else {
	    boxes = new JButton[nrOfOptions];
	    fields = new JTextField[nrOfOptions];
	}
	// //////////////////////////////////////////////////////////////////////////////

	// /////////////////////////Build window by positioning
	// JComponents//////////////////////////////////////////
	panel.setLayout(null);
	possibleAnswerLabel.setBounds(40, 15, 200, 15);
	panel.add(possibleAnswerLabel);

	for (int i = 0; i < nrOfOptions; i++) {
	    boxes[i] = new JButton();
	    boxes[i].setMargin(new Insets(0, 1, 0, 1));
	    boxes[i].setBounds(40, i * 35 + 39, 20, 20);
	    boxes[i].setBackground(new Color(255, 255, 255));
	    fields[i] = new JTextField(12);
	    fields[i].setBounds(70, i * 35 + 35, 200, 30);
	    panel.add(boxes[i]);
	    panel.add(fields[i]);
	}

	back.setBounds(40, nrOfOptions * 35 + 90, 80, 25);
	cancel.setBounds(280, nrOfOptions * 35 + 90, 80, 25);
	finish.setBounds(380, nrOfOptions * 35 + 90, 80, 25);

	panel.add(back);
	panel.add(cancel);
	panel.add(finish);

	this.add(panel);
	this.addActionListenerButtons(typeWindow);
	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	this.setVisible(true);
    }

    private void addActionListenerButtons(String typeWindow) {
	final boolean bool;

	if (typeWindow.equals("Text Answer"))
	    bool = true;
	else if (typeWindow.equals("Single Choice"))
	    bool = true;
	else
	    bool = false;
	for (int i = 0; i < boxes.length; i++) {
	    boxes[i].addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    if (bool)
			for (int j = 0; j < boxes.length; j++)
			    boxes[j].setBackground(new Color(255, 255, 255));
		    if (((JButton) e.getSource()).getBackground().equals(Color.white))
			((JButton) e.getSource()).setBackground(new Color(220, 220, 220));
		    else
			((JButton) e.getSource()).setBackground(new Color(255, 255, 255));
		}

	    });
	}
    }

    public JButton getBack() {
	return back;
    }

    public JButton getCancel() {
	return cancel;
    }

    public JButton getFinish() {
	return finish;
    }

    public void closeFrame() {
	this.dispose();
    }

    public List<String> getAnswers() {
	List<String> answ = new ArrayList<String>();

	for (int i = 0; i < fields.length; i++)
	    answ.add(fields[i].getText());
	return answ;
    }

    public List<Integer> getCorrectIndexes() {
	List<Integer> ind = new ArrayList<Integer>();
	for (int i = 0; i < boxes.length; i++)
	    if (!boxes[i].getBackground().equals(Color.white))
		ind.add(i);
	return ind;
    }

    public int getCorrectIndex() {
	for (int i = 0; i < boxes.length; i++)
	    if (!boxes[i].getBackground().equals(Color.white))
		return i;
	return -1;
    }
}
